﻿using DiffLib;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace AssistCore
{
    public static class ExamTiKu
    {
        private static NameValueCollection tiku = new NameValueCollection();
        public static bool Load(string filePath)
        {
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {

                    BinaryFormatter bf = new BinaryFormatter();
                    NameValueCollection collection = (NameValueCollection)bf.Deserialize(fs);
                    tiku = collection;
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        public static bool Load(byte[] fileBytes)
        {
            try
            {
                using (MemoryStream ms = new MemoryStream(fileBytes))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    NameValueCollection collection = (NameValueCollection)bf.Deserialize(ms);
                    tiku = collection;
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        public static string GetAnswer(string timu, string[] answerList, out int matchIndex)
        {
            var answers = tiku.GetValues(timu);
            var minNotMatch = 99;
            var minMatchIndex = 0;
            var minNotMatchStr = string.Empty;
            for (var i = 0; i < answers.Length; i++)
            {
                for (var j = 0; j < answerList.Length; j++)
                {
                    var diff = Diff.CalculateSections(answers[i].ToCharArray(), answerList[j].ToCharArray());
                    var notMatch = 0;
                    foreach (var section in diff)
                    {
                        if (!section.IsMatch)
                        {
                            notMatch += section.LengthInCollection1;
                        }
                    }
                    if (notMatch < minNotMatch)
                    {
                        minNotMatch = notMatch;
                        minNotMatchStr = answers[i];
                        minMatchIndex = j;
                    }
                }
            }
            matchIndex = minMatchIndex;
            return minNotMatchStr;
        }
        public static string GetAnswer(string timu)
        {
            return tiku.Get(timu);
        }
        public static string[] GetAllTiMu()
        {
            return tiku.AllKeys;
        }
        public static string GetSimilarTiMu(string str)
        {
            var minNotMatch = str.Length;
            var minNotMatchStr = string.Empty;
            var tiku = GetAllTiMu();
            foreach (var item in tiku)
            {
                var diff = Diff.CalculateSections(str.ToCharArray(), item.ToCharArray());
                var notMatch = 0;
                foreach (var section in diff)
                {
                    if (!section.IsMatch)
                    {
                        notMatch += Math.Max(section.LengthInCollection1, section.LengthInCollection2);
                    }
                }
                if (notMatch < minNotMatch)
                {
                    minNotMatch = notMatch;
                    minNotMatchStr = item;
                }
            }
            return minNotMatchStr;
        }
        public static string GetSimilarTiMu(string str, string[] tiku)
        {
            var minNotMatch = str.Length;
            var minNotMatchStr = string.Empty;
            foreach (var item in tiku)
            {
                var diff = Diff.CalculateSections(str.ToCharArray(), item.ToCharArray());
                var notMatch = 0;
                foreach (var section in diff)
                {
                    if (!section.IsMatch)
                    {
                        notMatch += section.LengthInCollection1;
                    }
                }
                if (notMatch < minNotMatch)
                {
                    minNotMatch = notMatch;
                    minNotMatchStr = item;
                }
            }
            return minNotMatchStr;

        }
        public static string GetSimilarText(string str, string[] source, out int index)
        {
            var minNotMatch = str.Length;
            var minNotMatchPos = 0;
            var minNotMatchStr = string.Empty;
            var i = 0;
            foreach (var item in source)
            {
                var diff = Diff.CalculateSections(str.ToCharArray(), item.ToCharArray());
                var notMatch = 0;
                foreach (var section in diff)
                {
                    if (!section.IsMatch)
                    {
                        notMatch += section.LengthInCollection1;
                    }
                }
                if (notMatch < minNotMatch)
                {
                    minNotMatch = notMatch;
                    minNotMatchPos = i;
                    minNotMatchStr = item;
                }
                i++;
            }
            index = minNotMatchPos;
            return minNotMatchStr;
        }
        public static void SerializeMethod(NameValueCollection list, string filePath)
        {
            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, list);
            }
        }
        public static void MakeTiku(string filePath, string dictPath)
        {
            var col = new NameValueCollection();
            var lines = File.ReadAllLines(filePath);
            for (var i = 0; i < lines.Length; i++)
            {
                var qa = lines[i].Split(new string[] { "|||||" }, StringSplitOptions.RemoveEmptyEntries);
                if (qa.Length != 2)
                {
                    continue;
                }
                var q = qa[0];
                var a = qa[1];
                col.Add(q, a);
            }
            SerializeMethod(col, dictPath);
        }
    }
}
